<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Controller {

	public $has_many = array('user_roles_m');
	
    public function __construct()
    {
        parent::__construct();
		$this->record_per_page = $this->system_library->settings['record_per_page'];
        $this->load->library('users/auth');
		$this->load->library('form_validation');
		
		$this->load->model('users_m', 'users');
		
		$this->lang->load('user');
		
		$this->template
				->set_theme('default')
				->set_layout('default')
				->append_metadata($this->asset->css('admin/default.css'))
				->append_metadata($this->asset->css('notifcation.slider.css'))
				->append_metadata($this->asset->css('admin/jquery.accordion.css'))
				->append_metadata($this->asset->css('reset.css'))
				->append_metadata($this->asset->css('general.css'))
				->append_metadata($this->asset->js('jquery/jquery-1.6.2.min.js'))
				->append_metadata($this->asset->js('jquery_accordion_init.js'))
				->append_metadata($this->asset->js('jquery.freeow.js'))
				->append_metadata($this->asset->js('notificationBox.jquery.js'))
				->set_partial('header', 'partials/header')
				->set_partial('breadcrumb','partials/breadcrumb')
                ->set_partial('footer', 'partials/footer')
				->set_partial('navigation', 'partials/admin/menu')
				->set_breadcrumb('Home', 'admin/dashboard');
    }
    
	public function index()
	{
       $this->login();
	}
    
    public function login()
    {	
        // Get the user data
		$user_data = (object) array(
			'email'		=> $this->input->post('email'),
			'password'	=> $this->input->post('password')
		);

		$validation = array(
			array(
				'field' => 'email',
				'label' => lang('user_email_label'),
				'rules' => 'required|trim|callback__check_login'
			),
			array(
				'field' => 'password',
				'label' => lang('user_password_label'),
				'rules' => 'required|min_length[6]|max_length[20]'
			),
		);
		
		// Set the validation rules
		$this->form_validation->set_rules($validation);
		
		if ($this->form_validation->run())
		{
			if ($this->session->userdata('logged_in') === TRUE)
			{
				redirect('users/app', 'refresh');
			}
		}
		
		$this->template
                ->set_theme('default')
				->append_metadata($this->asset->css('general.css'))	
                ->set_partial('header', 'partials/header')
                ->set_partial('footer', 'partials/footer')
                ->set_layout('default')
				->build('login');
    }
	
	public function lists($page = 0)
	{
		$this->lang->load('user');
		$this->load->library('pagination');
		
		if ($page < 0  || strlen($page) > 13)
		{
			redirect('users/lists');
		}
		else
		{
			$limit = $this->record_per_page;
			
			$users = new Users_m();
			
			$total_rows = $users->where('is_active', '1')->get(($limit + $page), $page)->count();
			
			if($total_rows < $page)
			{
				redirect('users/lists');
			}
			else
			{
				$data['users']				= $users->where('is_active', '1')->get($limit, $page);
				
				//setup pagination
				$config['base_url']			= site_url('users/lists/');
				$config['per_page']			= $limit;
				$config['uri_segment'] 		= 4;
				$config['total_rows']		= $total_rows;
				$config['anchor_class'] 	= 'class="btn"';
				$config['cur_tag_open'] 	= ' <div class="btn btn-disabled">';
				$config['cur_tag_close'] 	= '</div>';
				
				$this->pagination->initialize($config);
				
				$data['page'] 				= $this->pagination->create_links();
				
				$this->template
							->set_breadcrumb('Users','')
							->build('users/manage/list', $data);
			}
		}
	}
	
	public function create()
	{
		if (!$this->auth->is_logged_in())
		{
			redirect('users/login', 'refresh');
		}
		
		$this->lang->load('admin/admin');
		
		$validation = array(
							array(
								'field' => 'firstname',
								'label' => lang('user_firstname_label'),
								'rules' => 'required|min_length[1]|max_length[60]'
							),
							array(
								'field' => 'lastname',
								'label' => lang('user_lastname_label'),
								'rules' => 'required|min_length[1]|max_length[60]'
							),
							array(
								'field' => 'username',
								'label' => lang('user_username_label'),
								'rules' => 'required|alphanumeric|min_length[3]|max_length[20]|callback__check_username'
							),
							array(
								'field' => 'email',
								'label' => lang('user_email_label'),
								'rules' => 'required|valid_email|callback__check_email'
							),
							array(
								'field' => 'password',
								'label' => lang('user_password_label'),
								'rules' => 'required|min_length[6]|max_length[20]'
							),
							array(
								'field' => 'confirm_password',
								'label' => lang('user_confirm_password_label'),
								'rules' => 'required|matches[password]'
							),
							array(
								'field' => 'display_name',
								'label' => lang('user_display_name_label'),
								'rules' => 'required|alphanumeric|min_length[3]|max_length[50]'
							),
						);
		
		$user_data = array(
						   'firstname'		=> $this->input->post('firstname'),
						   'lastname'		=> $this->input->post('lastname'),
						   'email'			=> $this->input->post('email'),
						   'username'		=> $this->input->post('username'),
						   'display_name'	=> $this->input->post('display_name'),
						   'password'		=> $this->input->post('password'),
						   'role_id'		=> $this->input->post('role'),
						   'is_active'		=> $this->input->post('is_active') != '1' ? '0' : '1'
					);
		
		$this->form_validation->set_rules($validation);
		
		if ($this->form_validation->run())
		{
			$id = $this->users->create_user($user_data);
			
			if ($id)
			{
				$this->session->set_flashdata('success', lang('user_create_new_success'));
				redirect('users/create', 'refresh');
			}
			else
			{
				$this->session->set_flashdata('error', lang('user_create_new_failed'));
			}
		}
		
		$this->load->model('roles_m', 'roles');
		
		$roles = $this->roles->get_roles();
		
		$this->template
				->set_breadcrumb('Users','users/lists/')
				->set_breadcrumb('Create')
				->append_metadata($this->asset->js('jscripts/tiny_mce/tiny_mce.js'))
				->append_metadata($this->asset->js('tiny_mce_init.js'))
				->append_metadata($this->asset->js('jquery.tab.init.js'))
				->append_metadata($this->asset->js('jquery/facebox.js'))
				->append_metadata($this->asset->css('admin/jquery.tab.css'))
				->append_metadata($this->asset->css('facebox.css'))
                ->set_partial('header', 'partials/header')
                ->set_partial('footer', 'partials/footer')
				->set_partial('navigation', 'partials/admin/menu')
                ->set_layout('default')
				->set('roles', $roles)
				->build('users/manage/create');
	}
	
	public function edit($id = NULL)
	{
		
	}
	
	public function logout()
	{
		$this->auth->logout();
		$this->session->set_flashdata('success', lang('user_logged_out'));
		redirect('users/login', 'refresh');
	}
	
	public function _check_username()
	{
		if ($this->users->check_username($this->input->post('username')))
		{
			$this->form_validation->set_message('_check_username', lang('user_create_username_exist'));
			return FALSE;
		}
		
		return TRUE;
	}
	
	public function _check_email()
	{
		if ($this->users->check_email($this->input->post('email')))
		{
			$this->form_validation->set_message('_check_email', lang('user_create_email_already_used'));
			return FALSE;
		}
		
		return TRUE;
	}
    
    public function _check_login($email)
	{
		if ($this->auth->login($email, $this->input->post('password')))
		{
			return TRUE;
		}
		
		$this->form_validation->set_message('_check_login', lang('user_login_unsuccessful'));
		return FALSE;
    }
}

/* End of file users.php */
/* Location: ./application/modules/users/controllers/users.php */